package client;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

import common.Game;
import common.IServer;
import common.IUI;


public class Client {

    /* TODO add a catch to when a user tries to connect twice with the same port number. */
    /* TODO we should take most of these parameters from user */
    /* we should also get user external ip somehow */
    private static final String HOST2 = "delta-tomcat-vm.cs.tau.ac.il";
    private static final String HOST1 = "localhost";
    private static final int REGISTRY_PORT = 40756;
    private static Registry registry;
    
    public static void main(String[] args) {
        
        ConsoleUI playerUI = new ConsoleUI();
        playerUI.printTextFile("resources/welcome.aqr");
        
        int decision = playerUI.getUserGameTypeDecision();
        switch (decision) {
        case 1:
            localGame(playerUI);
            break;
        case 2:
	        try {
				onlineGame(playerUI);
				break;
			} catch (RemoteException e) {
				playerUI.printError(2);
				e.printStackTrace();
			}
        case 3:
        	playerUI.printMessage(38);
        	break;
        }
        
    }

	public static void onlineGame(ConsoleUI playerUIImpl) throws RemoteException {
		int decision = playerUIImpl.getHostAddressDecision();
		int port = playerUIImpl.getPortDecision();
		playerUIImpl.printMessage(27);
		if (decision == 1) {
			registry = LocateRegistry.getRegistry(HOST1, REGISTRY_PORT);
		} else {
			registry = LocateRegistry.getRegistry(HOST2, REGISTRY_PORT);
		}
		IServer remoteServer = null;
		try {
			remoteServer = (IServer) registry.lookup("Server");
		} catch (NotBoundException e1) {
			/* 
			 * this can never happen.
			 * if server is not connected a RemoteException is thrown 
			 */
		}

		String clientAddress = remoteServer.getClientIP();
		if (clientAddress == null) {
		    playerUIImpl.printMessage("There was a problem connecting... goodbye.");
		    return;
		}
		
		System.setProperty("java.rmi.server.hostname", clientAddress);
		IUI playerUI;
		if (port == -1) {
			playerUI = (IUI) UnicastRemoteObject.exportObject(playerUIImpl, 0);
		} else {
			playerUI = (IUI) UnicastRemoteObject.exportObject(playerUIImpl, port);
		}
		playerUIImpl.printMessage(28);
		remoteServer.joinServer(playerUI);
	}
	
	private static void localGame(ConsoleUI playerUI) {
        String name;
        List<String> namesList = new ArrayList<String>();
        List<IUI> UIList = new ArrayList<IUI>();
        
        playerUI.setLocalGame(true);
        
        int numOfPlayers = playerUI.getNumberOfPlayers();
        
        int i=1;
        do {
            name  = playerUI.getPlayerName(i);
            if (namesList.contains(name)) {
                playerUI.printMessage(25); // TODO when we can pass strings we should pass the chosen name
            } else {
                namesList.add(name);
                i++;
            }
        } while (i <= numOfPlayers);
        
        for (int j = 0; j < numOfPlayers; j++) {
        	UIList.add(playerUI);
		}
        
        Game NewGame = new Game(namesList, UIList, true);
        NewGame.startGame();
        
	}
	
    //pigumim for testing
    
    public Registry getRegistry()
    {
    	return registry;
    }
	
    
}
